<?php

// Created by Khanh Nam
class DispatchController extends Controller{
    public $layout='//layouts/sp';
    public function filters(){
        return array(
            'accessControl'
        );
    }
    public function accessRules(){
        return array(
            array(
                'allow',
                'users'=>array('@'),
            ),
            array(
                'deny',
                'roles'=>array('*')
            )
        );
    }
    public function actionIndex(){
        $this->render('index');
    }
    public function actionList($page=0,$sort=NULL){
        if($page>0) $page--;
        $order=CJSON::decode($sort);

        $data=new CActiveDataProvider('Dispatchdetails',array(
            'criteria'=>array(
                'condition'=>'complete = 0',
                'order'=>isset($order)?$order[0]['property'].' '.$order[0]['direction']:NULL
            ),
            'pagination'=>array(
                'pageSize'=>20,
                'currentPage'=>$page
            )
        ));

        if($data!=NULL && count($data->getData())>0){
            $rs=array();
            $rs['totalCount']=$data->getTotalItemCount();
            foreach($data->getData() as $i=>$item){

                $rs['data'][$i]=$item->getAttributes();
                $rs['data'][$i]['custno']=$item->customer->lastname;
                $rs['data'][$i]['techStatus']=$item->lastestTech!=NULL?$item->lastestTech->status:NULL;
                $rs['data'][$i]['task']=$item->lastestTask!=NULL?$item->lastestTask->taskname:NULL;
                $rs['data'][$i]['custCity']=$item->customer->city;
                $rs['data'][$i]['custAdd']=$item->customer->add1;
                $rs['data'][$i]['custState']=$item->customer->state;
            }   
            echo json_encode($rs);
        }
        Yii::app()->end();
    }
}
?>
